<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
class Config extends Common
{
  public function index(){
      if (request()->isPost()) {
          $data=input('post.');
          $file = request()->file('logoimg');
          if ($file) {
            $data['logo']=  $this->loadimg($file);
          }
          $res=db('config')->find();
          if (!$res) {
              db('config')->insert(['config'=>json_encode($data,JSON_UNESCAPED_UNICODE)]);
          }else {
             $result=db('config')->where('id',$res['id'])->update(['config'=>json_encode($data,JSON_UNESCAPED_UNICODE)]);
             if (!$result) {
                 $this->error('配置信息修改失败','config/index');
             }
          }
      }
      $config=db('config')->find();
      $this->assign('config',json_decode($config['config'],true));
      return view();
  }
  public function loadimg($file){
        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
        if($info){
            return $info->getSaveName();
        }
  }
}